home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-09-21 | 1.6 KB | 65 lines | [TEXT/ttxt] |
- //-------------------------------------------------------------------//
- //LAGRANGE Lagrange interpolation of arbitrary order. Y = LAGRANGE(TAB,X0,N)
- // returns an N-th order interpolated value from table TAB, looking
- // up X0 in the first column of TAB.
-
- // NOTE: TAB's 1st column is checked for monotonicity. It is an
- // error to request a value outside the range of the first column
- // of TAB for X0.
-
- // Original Author: Michael F. Saucier 10-16-87
- //-------------------------------------------------------------------//
-
- lagrange = function (tab, x0, N)
- {
-
- local (dx, i, j, jj, jmin, lden, lnum, m, n, seq, sig, tab2, y0);
-
- if (!exist (N)) {
- error("Wrong number of input arguments.");
- }
-
- dx = diff (tab[;1]);
- sig = sign (dx[1]);
- if (any (sign (dx) - sig))
- {
- error("First column of the table must be monotonic.");
- }
-
- // Check range of 1st column versus x0
-
- if (tab[1;1] > x0 || tab[tab.nr;1] < x0) {
- error ("lagrange: x0 must be within range of tab");
- }
-
- i = find (tab[;1] == x0);
- if (i != 0)
- {
- return tab[i;2];
- }
-
- m = size (tab)[1]; n = size (tab)[2];
- jmin = min ([max ([min (find (tab[;1] > x0)) - fix ((N+1)/2),1]), m - N]);
- tab2 = tab[jmin:jmin+N;];
- jj = 1:N+1;
-
- seq = x0*ones (1, N+1) - tab2[jj;1]';
- lnum = prod (seq) ./ seq;
-
- lden = ones (1, N+1);
- for (i in jj)
- {
- for (j in jj)
- {
- if (j != i)
- {
- # fprintf ("stderr", "i = %i, j = %i\n", i, j);
- lden[i] = lden[i] * (tab2[i;1] - tab2[j;1]);
- }
- }
- }
-
- y0 = sum (lnum' ./ lden' .* tab2[jj;2]);
- return y0;
- };
-